package com.woniu.defecttracking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.defecttracking.annotation.AddNum;
import com.woniu.defecttracking.commens.ComUtil;
import com.woniu.defecttracking.dto.FindPriorityDto;
import com.woniu.defecttracking.entity.Priority;
import com.woniu.defecttracking.exception.DefectTrackingException;
import com.woniu.defecttracking.mapper.PriorityMapper;
import com.woniu.defecttracking.param.AddPriorityParam;
import com.woniu.defecttracking.param.FindPriorityParam;
import com.woniu.defecttracking.service.PriorityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2021-12-31
 */
@Service
public class PriorityServiceImpl extends ServiceImpl<PriorityMapper, Priority> implements PriorityService {

    @Override
    public FindPriorityDto findPriority() {
        QueryWrapper<Priority> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isdel",0);
        queryWrapper.eq("status",0);
        List<Priority> list = list(queryWrapper);
        FindPriorityDto findPriorityDto = new FindPriorityDto();
        findPriorityDto.setPriorities(list);
        return findPriorityDto;
    }

    @AddNum("priorityNum")
    @Override
    public boolean addPriority(AddPriorityParam addPriorityParam) {

        Priority priority = ComUtil.copyProperties(addPriorityParam, Priority.class);

        return save(priority);
    }

    @Override
    public boolean updatePriority(AddPriorityParam addPriorityParam) {


        Priority update = ComUtil.copyProperties(addPriorityParam, Priority.class);
        boolean b = updateById(update);
        return b;
    }

    @Override
    public boolean deletePriority(List<Integer> ids) throws DefectTrackingException{
        List<Priority> priorities = listByIds(ids);
        for (Priority priority : priorities) {
            if (priority.getIsdel()==1){
                throw new DefectTrackingException("删除失败",433);
            }
        }
        priorities.forEach(a->a.setIsdel(1));
        saveOrUpdateBatch(priorities);
        return true;
    }

    @Override
    public boolean updateStatusUp(Integer id) {
        QueryWrapper<Priority> objectQueryWrapper = new QueryWrapper<>();
        Priority savarityId = getOne(objectQueryWrapper.eq("priority_id",id));
        if (savarityId.getStatus()==0){
            return false;
        }
        savarityId.setStatus(0);
        if (updateById(savarityId)){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateStatusDown(Integer id) {
        QueryWrapper<Priority> objectQueryWrapper = new QueryWrapper<>();
        Priority priorityId = getOne(objectQueryWrapper.eq("priority_id",id));
        if (priorityId.getStatus()==1){
            return false;
        }
        priorityId.setStatus(1);
        if (updateById(priorityId)){
            return true;
        }
        return false;
    }

    @Override
    public FindPriorityDto findAllPriority() {
        QueryWrapper<Priority> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isdel",0);


        List<Priority> list = list(queryWrapper);
        FindPriorityDto findPriorityDto = new FindPriorityDto();
        findPriorityDto.setPriorities(list);
        return findPriorityDto;
    }
}
